package cn.chiship.framework.third.core.common;

import cn.chiship.framework.third.biz.wxpub.entity.ThirdWechatMenu;
import cn.chiship.sdk.third.wx.core.common.WxPubCommonConstants;
import cn.chiship.sdk.third.wx.core.entity.pub.Button;
import cn.chiship.sdk.third.wx.core.entity.pub.ClickButton;
import cn.chiship.sdk.third.wx.core.entity.pub.MiniprogramButton;
import cn.chiship.sdk.third.wx.core.entity.pub.ViewButton;


import java.util.Arrays;
import java.util.List;

/**
 * 根据WechatMenu的type类型为相应的button类赋值
 * @author lijian
 */
public class AssignButton {
    public static Button menuToButton(ThirdWechatMenu wechatMenu) {
        if (WxPubCommonConstants.MENU_CLICK.equals(wechatMenu.getType())) {
            ClickButton clickButton = new ClickButton();
            clickButton.setName(wechatMenu.getName());
            clickButton.setType(wechatMenu.getType());
            clickButton.setKey(wechatMenu.getEventValue());
            return clickButton;
        } else if (WxPubCommonConstants.MENU_VIEW.equals(wechatMenu.getType())) {
            ViewButton viewButton = new ViewButton();
            viewButton.setName(wechatMenu.getName());
            viewButton.setType(wechatMenu.getType());
            viewButton.setUrl(wechatMenu.getEventValue());
            return viewButton;
        } else if (WxPubCommonConstants.MENU_MINIPROGRAM.equals(wechatMenu.getType())) {
            MiniprogramButton miniprogramButton = new MiniprogramButton();
            miniprogramButton.setName(wechatMenu.getName());
            miniprogramButton.setType(wechatMenu.getType());
            List<String> eventValue = Arrays.asList(wechatMenu.getEventValue().split("\\|"));
            miniprogramButton.setUrl(eventValue.get(0));
            miniprogramButton.setAppid(eventValue.get(1));
            miniprogramButton.setPagepath(eventValue.get(2));
            return miniprogramButton;
        }
        return null;
    }
}
